
/**
 * Created by admin on 2016/10/29 0029.
 */
package html;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;
import util.DateUtil;
import util.DbUtil;

import java.io.IOException;

public class Spider {

    public static void main(String args[]) {

//        String url_str = "http://news.hfut.edu.cn/list-2-1.html";
//        URL url = null;
//        try {
//            url = new URL(url_str);
//        } catch (MalformedURLException e) {
//            e.printStackTrace();
//        }
//
//        String charset = "utf-8";
//        int sec_cont = 1000;
//        try {
//            URLConnection url_con = url.openConnection();
//            url_con.setDoOutput(true);
//            url_con.setReadTimeout(10 * sec_cont);
//            url_con.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1)");
//            InputStream htm_in = url_con.getInputStream();
//            String htm_str = InputStream2String(htm_in, charset);

//        } catch (IOException e) {
//            e.printStackTrace();
//        }
        Document doc=null;
        try {
            doc= Jsoup.connect("http://news.hfut.edu.cn/list-2-1.html").get();
        } catch (IOException e) {
            e.printStackTrace();
        }
        Elements elements1 = doc.getElementsByClass("lh30");
        Elements elements2 = elements1.select("span");
        Elements elements3 =elements1.select("a");
        for(int i=1;i<elements2.size();i++){
            News news=new News();
            news.setTitle(elements3.get(i).attr("title"));
            if(elements3.get(i).attr("href").contains("http")){
                news.setUrl(elements3.get(i).attr("href"));
            }else{
                news.setUrl("http://news.hfut.edu.cn"+elements3.get(i).attr("href"));
            }

            String dateStr=elements2.get(i).text();
            DateUtil dateUtil=new DateUtil();
            news.setLongTime(dateUtil.dateToLong(dateUtil.strToDate(dateStr)));
            if(DbUtil.queryByTitle(elements3.get(i).attr("title"))==null){
                DbUtil.addNews(news);
            }
        }
        System.out.println("更新完毕！");
    }


//    /**
//     * Method: InputStream2String
//     * Description: make InputStream to String
//     *
//     * @param in_st   inputstream which need to be converted
//     * @param charset encoder of value
//     * @throws IOException if an error occurred
//     */
//    public static String InputStream2String(InputStream in_st, String charset) throws IOException {
//        BufferedReader buff = new BufferedReader(new InputStreamReader(in_st, charset));
//        StringBuffer res = new StringBuffer();
//        String line = "";
//        while ((line = buff.readLine()) != null) {
//            res.append(line);
//        }
//        return res.toString();
//    }

}
